<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ConcurrentHashMap 深度解析 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8f9fa;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #6e48aa 0%, #9d50bb 100%);
            color: white;
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            border-radius: 12px;
            overflow: hidden;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.1);
        }
        .code-block {
            font-family: 'Courier New', Courier, monospace;
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 8px;
            padding: 1rem;
            overflow-x: auto;
        }
        .hover-grow {
            transition: transform 0.3s ease;
        }
        .hover-grow:hover {
            transform: scale(1.02);
        }
        .timeline-item {
            position: relative;
            padding-left: 2rem;
            margin-bottom: 2rem;
        }
        .timeline-item::before {
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            width: 4px;
            height: 100%;
            background: linear-gradient(to bottom, #9d50bb, #6e48aa);
            border-radius: 2px;
        }
        .timeline-badge {
            position: absolute;
            left: -12px;
            top: 0;
            width: 24px;
            height: 24px;
            border-radius: 50%;
            background-color: #9d50bb;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-5xl mx-auto text-center">
            <h1 class="text-4xl sm:text-5xl lg:text-6xl font-bold mb-6 font-serif">
                <span class="inline-block">ConcurrentHashMap</span>
            </h1>
            <p class="text-xl sm:text-2xl mb-8 max-w-3xl mx-auto leading-relaxed">
                Java 并发编程中的高效线程安全哈希表实现
            </p>
            <div class="flex justify-center space-x-4">
                <a href="#structure" class="px-6 py-3 bg-white text-purple-700 font-medium rounded-full hover:bg-opacity-90 transition">
                    <i class="fas fa-project-diagram mr-2"></i>数据结构
                </a>
                <a href="#evolution" class="px-6 py-3 bg-white text-purple-700 font-medium rounded-full hover:bg-opacity-90 transition">
                    <i class="fas fa-history mr-2"></i>版本演进
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8 card">
                <h2 class="text-2xl font-bold mb-4 text-purple-800"><i class="fas fa-info-circle mr-2"></i>概述</h2>
                <p class="mb-4">ConcurrentHashMap 是 Java 并发包中的一个高效线程安全的哈希表实现，旨在支持高并发操作，提供了较好的性能和可伸缩性。它的内部数据结构和设计使其能够在多线程环境下实现高效的并发访问。</p>
                <div class="flex flex-wrap gap-4 mt-6">
                    <div class="flex items-center bg-purple-100 px-4 py-2 rounded-full">
                        <i class="fas fa-shield-alt text-purple-600 mr-2"></i>
                        <span>线程安全</span>
                    </div>
                    <div class="flex items-center bg-purple-100 px-4 py-2 rounded-full">
                        <i class="fas fa-bolt text-purple-600 mr-2"></i>
                        <span>高并发</span>
                    </div>
                    <div class="flex items-center bg-purple-100 px-4 py-2 rounded-full">
                        <i class="fas fa-expand text-purple-600 mr-2"></i>
                        <span>可扩展</span>
                    </div>
                </div>
            </div>
        </section>

        <!-- Data Structure -->
        <section id="structure" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-purple-800 border-b pb-2">基本数据结构</h2>
            
            <!-- Segment Structure -->
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-bold mb-4 text-purple-700 flex items-center">
                        <i class="fas fa-puzzle-piece mr-2"></i>
                        <span>Segment（分段）</span>
                    </h3>
                    <p class="mb-4">在 Java 7 及之前版本，ConcurrentHashMap 使用分段锁（Segment）来实现并发控制。Segment 是一种轻量级的锁，每个 Segment 锁定哈希表的一个部分，允许多个线程并发访问不同的段。</p>
                    <p class="font-medium mb-2">数据结构：</p>
                    <div class="code-block mb-4">
                        final Segment&lt;K,V&gt;[] segments;
                    </div>
                    <p>segments 是一个 Segment 数组，每个 Segment 负责哈希表的一个部分。对每个段的操作（如 put, get 等）只会锁定该段，允许其他段的操作并发执行。</p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="mermaid">
                        graph TD
                            A[ConcurrentHashMap] --> B[Segment 0]
                            A --> C[Segment 1]
                            A --> D[Segment 2]
                            A --> E[...]
                            B --> F[HashEntry Array]
                            C --> G[HashEntry Array]
                            D --> H[HashEntry Array]
                            F --> I[Key-Value Pairs]
                            G --> J[Key-Value Pairs]
                            H --> K[Key-Value Pairs]
                    </div>
                </div>
            </div>

            <!-- Node Structure -->
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-bold mb-4 text-purple-700 flex items-center">
                        <i class="fas fa-link mr-2"></i>
                        <span>Node（节点）</span>
                    </h3>
                    <p class="mb-4">每个 Segment 中的数据结构是一个 Node 链表，存储键值对。节点结构通常包含键、值、哈希值和指向下一个节点的指针。</p>
                    <div class="code-block">
                        static final class Node&lt;K,V&gt; {
                            final K key;
                            volatile V value;
                            final int hash;
                            final Node&lt;K,V&gt; next;
                            
                            Node(K key, V value, int hash, Node&lt;K,V&gt; next) {
                                this.key = key;
                                this.value = value;
                                this.hash = hash;
                                this.next = next;
                            }
                        }
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card flex items-center">
                    <div class="mermaid">
                        flowchart LR
                            subgraph Node
                                A[key] --> B[value]
                                A --> C[hash]
                                A --> D[next]
                            end
                    </div>
                </div>
            </div>
        </section>

        <!-- Java 8 Implementation -->
        <section id="evolution" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-purple-800 border-b pb-2">Java 8 及以后实现</h2>
            <p class="mb-8 text-lg">在 Java 8 中，ConcurrentHashMap 进行了重构，放弃了分段锁，改为使用 synchronized 和 CAS（Compare-And-Swap）操作结合的机制，进一步提高并发性能。</p>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card hover-grow">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-archive"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-purple-700">桶（Buckets）</h3>
                    <p>ConcurrentHashMap 维护一个数组，数组的每个元素是一个链表或树形结构（红黑树）。</p>
                    <div class="code-block mt-4">
                        final Node&lt;K,V&gt;[] table;
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card hover-grow">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-sitemap"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-purple-700">链表和红黑树</h3>
                    <p>当某个桶中的链表长度超过特定阈值（默认为 8），链表会转换为红黑树，以提供更高效的查询操作。</p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card hover-grow">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-lock"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-purple-700">同步机制</h3>
                    <p>使用 synchronized 块来保证对桶的线程安全访问，和 CAS 操作来减少锁的竞争。</p>
                    <div class="code-block mt-4">
                        synchronized (bucketLock) {
                            // 对桶内链表或红黑树的操作
                        }
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-md p-6 card mb-8">
                <h3 class="text-xl font-bold mb-4 text-purple-700">红黑树节点结构</h3>
                <div class="code-block">
                    static final class TreeNode&lt;K,V&gt; extends Node&lt;K,V&gt; {
                        TreeNode&lt;K,V&gt; parent;
                        TreeNode&lt;K,V&gt; left;
                        TreeNode&lt;K,V&gt; right;
                        boolean red;
                        
                        TreeNode(K key, V value, int hash, Node&lt;K,V&gt; next) {
                            super(key, value, hash, next);
                        }
                    }
                </div>
            </div>
        </section>

        <!-- Evolution Timeline -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-purple-800 border-b pb-2">版本演进时间线</h2>
            
            <div class="bg-white rounded-xl shadow-md p-8 card">
                <div class="timeline-item">
                    <div class="timeline-badge">
                        <i class="fas fa-code-branch"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2 text-purple-700">Java 7 及之前</h3>
                    <p class="text-gray-600 mb-2">分段锁（Segment）实现</p>
                    <ul class="list-disc pl-5 space-y-1">
                        <li>使用 Segment 数组分割哈希表</li>
                        <li>每个 Segment 有自己的锁</li>
                        <li>并发度取决于 Segment 数量</li>
                    </ul>
                </div>
                
                <div class="timeline-item">
                    <div class="timeline-badge">
                        <i class="fas fa-rocket"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2 text-purple-700">Java 8</h3>
                    <p class="text-gray-600 mb-2">重大重构</p>
                    <ul class="list-disc pl-5 space-y-1">
                        <li>移除 Segment，使用 synchronized + CAS</li>
                        <li>引入链表转红黑树机制</li>
                        <li>更细粒度的锁控制</li>
                        <li>性能显著提升</li>
                    </ul>
                </div>
                
                <div class="timeline-item">
                    <div class="timeline-badge">
                        <i class="fas fa-cogs"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2 text-purple-700">后续版本优化</h3>
                    <p class="text-gray-600 mb-2">持续改进</p>
                    <ul class="list-disc pl-5 space-y-1">
                        <li>扩容机制优化</li>
                        <li>哈希算法改进</li>
                        <li>内存占用优化</li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Resize Mechanism -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-purple-800 border-b pb-2">扩容机制</h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-bold mb-4 text-purple-700 flex items-center">
                        <i class="fas fa-expand-alt mr-2"></i>
                        <span>动态扩容</span>
                    </h3>
                    <p class="mb-4">当 ConcurrentHashMap 中的元素数量增加到一定程度时，会进行扩容。扩容时，会重新计算每个键的哈希值并将其重新分配到新的桶中。</p>
                    <div class="code-block">
                        void resize() {
                            // 创建新桶数组
                            // 重新计算和分配元素到新数组
                        }
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card flex items-center">
                    <div class="mermaid">
                        flowchart LR
                            A[当前桶数组] -- 元素数量 > 阈值 --> B[创建新数组]
                            B --> C[重新哈希]
                            C --> D[元素迁移]
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary -->
        <section class="mb-16">
            <div class="bg-gradient-to-r from-purple-600 to-purple-800 rounded-xl shadow-md p-8 text-white card">
                <h2 class="text-2xl font-bold mb-4">总结</h2>
                <p class="mb-6">ConcurrentHashMap 是 Java 并发编程中不可或缺的重要数据结构，通过巧妙的锁机制和数据结构设计，实现了高并发下的线程安全和性能平衡。从 Java 7 的分段锁到 Java 8 的 synchronized+CAS 优化，展示了 Java 并发编程技术的演进历程。</p>
                <div class="flex flex-wrap gap-4">
                    <div class="flex items-center bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-check-circle mr-2"></i>
                        <span>线程安全</span>
                    </div>
                    <div class="flex items-center bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-tachometer-alt mr-2"></i>
                        <span>高性能</span>
                    </div>
                    <div class="flex items-center bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-users mr-2"></i>
                        <span>高并发</span>
                    </div>
                    <div class="flex items-center bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-memory mr-2"></i>
                        <span>内存高效</span>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold">技术小馆</h3>
                    <p class="text-gray-400">探索技术的无限可能</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-purple-300 hover:text-purple-100 transition duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        <span>技术小馆地址</span>
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-center text-gray-400 text-sm">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            },
            themeVariables: {
                primaryColor: '#9d50bb',
                primaryBorderColor: '#6e48aa',
                primaryTextColor: '#fff',
                lineColor: '#9d50bb'
            }
        });
    </script>
</body>
</html>